<template>
  <div>
    <div class="table-page-search-wrapper">
      <a-form-model
        ref="formModel"
        :model="form"
        layout="inline"
        :rules="{
          name: [
            {
              validator: validators.noWhitespace(),
              trigger: 'change',
            },
            {
              validator: validators.noIllegalString(),
              trigger: 'change',
            },
            {
              max: 10,
              message: '长度在 1 到 10 个字符',
              trigger: 'change',
            },
          ],
        }"
      >
        <a-row :gutter="[16, 16]" type="flex" justify="end">
          <a-col :md="6" :sm="8">
            <a-form-model-item prop="name" label="应用名称">
              <a-input autocomplete="off" v-model="form.name" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :md="6" :sm="8">
            <a-form-model-item prop="type" label="应用类型">
              <a-select
                :getPopupContainer="
                triggerNode => {
                  return triggerNode.parentNode || document.body;
                }"
                v-model="form.type"
              >
                <a-select-option
                    v-for="d in stateMap"
                    :key="d.id"
                    :value="d.id"
                >
                  {{ d.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col> -->
          <span
            style="float: left; overflow: hidden"
            class="table-page-search-submitButtons"
          >
            <a-col :md="12" :sm="24">
              <a-button type="primary" @click="loadData(true)" icon="search">
                查询
              </a-button>
              <a-button
                @click="resetSearchForm"
                icon="reload"
                style="margin-left: 8px"
              >
                重置
              </a-button>
            </a-col>
          </span>
        </a-row>
      </a-form-model>
    </div>

    <a-table
      ref="table"
      rowKey="id"
      size="middle"
      :columns="columns"
      :dataSource="gDataSource"
      :pagination="gPagination"
      @change="gHandleTableChange"
    >
      <span slot="sortCol" slot-scope="text, record, index">
        {{
          (gPagination.current - 1) * gPagination.pageSize + parseInt(index) + 1
        }}
      </span>
      <span slot="action" slot-scope="text, record">
        <a-button type="primary" icon="profile" size="small" @click="handleDetail(record)">监控详情</a-button>
      </span>
    </a-table>
    <detail-modal ref="detailModal"></detail-modal>
  </div>
</template>

<script>
import { GwListMixin } from "@/mixins/GwListMixin";
import { gPostAction } from "@/api/manage";
import DetailModal from "./modules/app/DetailModal";

export default {
  name: "App",
  mixins: [GwListMixin],
  components: {
    DetailModal,
  },
  data() {
    return {
      // 表头
      columns: [
        {
          title: "序号",
          dataIndex: "",
          key: "rowIndex",
          width: 50,
          scopedSlots: { customRender: "sortCol" },
        },
        {
          title: "应用名称",
          width: 200,
          dataIndex: "name",
        },
        // {
        //   title: "应用类型",
        //   dataIndex: "typeName",
        // },
        {
          title: "云平台资源",
          dataIndex: "cloudResourceName",
        },
        {
          title: "所属数据中心",
          dataIndex: "datacenterName",
        },
        {
          title: "操作",
          width: 100,
          dataIndex: "action",
          scopedSlots: { customRender: "action" },
        },
      ],
      url: {
        list: "/app/page",
        datacenterList: "/asset/pageDatacenter",
      },
      form: {
        type: "",
        name: "",
      },
      stateMap: [
        {
          id: '',
          name: "全部"
        },
        {
          id:0,
          name:"无状态"
        },
        {
          id:1,
          name: "有状态"
        }
      ],
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    loadData(isDoSearch) {
      this.$refs.formModel.validate((error) => {
        if (!error) {
          return;
        }
        this.gQueryParam.type = this.form.type;
        this.gQueryParam.name = this.form.name;
        if (isDoSearch) {
          this.gQueryParam.pageNo = 1;
          this.gPagination.current = 1;
        }
        this.gQueryParam.title = "精确管控与供电制冷联动-基础资源细粒度感知-指标监控-L4层：应用资源";
        this.gQueryParam.content = "查询应用资源监控指标列表";
        gPostAction(this.url.list, this.gQueryParam).then((res) => {
          const data = res.data;
          if (data && data.list) {
            this.gDataSource = data.list;
            this.gPagination.total = data.totalCount;
          }
        });
      });
    },
    handleDetail(data) {
      this.$refs.detailModal.showModal(data);
    },
    resetSearchForm() {
      this.form.name = "";
      this.form.type = "";
      this.$refs.formModel.resetFields();
    },
  },
};
</script>
